package com.nhncorp.pleiades.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SpaceEventFilter implements EventFilter {
  
  private List<EventService.Events> spaceEventList;
  private Set<Integer> blankEventSet = new TreeSet<Integer>();
  private Map<EventFilter.Operator, String> constraints;
  private String keyword;
  
  public static final Log LOG = LogFactory.getLog(SpaceEventFilter.class
      .getName());
  
  public SpaceEventFilter() {
    this.keyword = "";
    initDefaultConstraint(keyword);
    initBlankEventSet();
  }
  
  public SpaceEventFilter(String keyword) {
    this.keyword = keyword;
    initDefaultConstraint(keyword);
    initBlankEventSet();
  }
  
  private void initDefaultConstraint(String defaultKeyword) {
    spaceEventList = new ArrayList<EventService.Events>();
    for(EventService.Events entry : EventService.Events.values()) {
      spaceEventList.add(entry);
    }
    constraints = new ConcurrentHashMap<EventFilter.Operator, String>();
    constraints.put(EventFilter.Operator.ANY, defaultKeyword);
  }
  
  private void initBlankEventSet() {
    blankEventSet.add(EventService.Events.SPACE_WRITE.ordinal());
    blankEventSet.add(EventService.Events.SPACE_TAKE.ordinal());
    blankEventSet.add(EventService.Events.SPACE_SUBSCRIBE.ordinal());
    blankEventSet.add(EventService.Events.SPACE_UNSUBSCRIBE.ordinal());
  }
  
  public void addConstraint(EventFilter.Operator operator, String key) {
    constraints.put(operator, key);
  }
  
  public void addConstraint(Map<EventFilter.Operator, String> constraintMap) {
    constraints.putAll(constraintMap);
  }
  
  public void addConstraint(Constraint con) {
    constraints.putAll(con.getConstraints());
  }
  
  public void removeConstraint(EventFilter.Operator operator) {
    constraints.remove(operator);
  }
  
  public void removeConstraint(Constraint con) {
    Map<EventFilter.Operator, String> copiedConstraints = new HashMap<EventFilter.Operator, String>();    
    synchronized (constraints) {
      copiedConstraints.putAll(constraints);
      for(Map.Entry<EventFilter.Operator, String> entry : copiedConstraints.entrySet()) {
        constraints.remove(entry);
      }
    }
  }
  
  public void removeAll() {
    constraints.clear();
    constraints.put(EventFilter.Operator.ANY, keyword);
  }
  
  public String getConstraintKeyword(EventFilter.Operator operator) {
    return constraints.get(operator);
  }
  
  public Map<EventFilter.Operator, String> getAllConstraints() {
    return this.constraints;
  }  
  
  public String getKeyword() {
    return keyword;
  }

  public void setKeyword(String keyword) {
    this.keyword = keyword;
  }

  public List<EventService.Events> getLockEventList() {
    return this.spaceEventList;
  }

  public void setLockEventList(List<EventService.Events> spaceEventList) {
    this.spaceEventList = spaceEventList;
  }
}
